package com.orangelife.mobile.common.db;

import android.database.sqlite.SQLiteDatabase;

/**
 * Created by Administrator on 2015/12/11.
 * 数据库读写权限和关闭数据库
 */
public class DBOperate {
   private static DBOperate dbOperate;
   private static DBHelper dbHelper;
   private SQLiteDatabase sqLiteDatabase;
   public static synchronized DBOperate getInstance(DBHelper dbHelper){
        if(dbOperate==null){
           initDb(dbHelper);
        }
       return dbOperate;
   }

   private static void initDb(DBHelper helper){
       dbOperate = new DBOperate();
       dbHelper = helper;
   }

    /**
     * 获取一个可写的数据库操作实例
     */
    public synchronized SQLiteDatabase getWritableDatabase(){
        sqLiteDatabase = dbHelper.getWritableDatabase();
        return sqLiteDatabase;
    }

    /**
     * 获取一个可读的数据库操作实例
     */
    public synchronized SQLiteDatabase getReadableDatabase(){
        sqLiteDatabase = dbHelper.getReadableDatabase();
        return sqLiteDatabase;
    }

    /**
     * 关闭数据库
     */
    public synchronized void closeDatabase(){
        sqLiteDatabase.close();
    }
}
